package com.yun.zf.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yun.zf.admin.excelListener.UploadDeptListener;
import com.yun.zf.common.admin.domain.SysDept;
import com.yun.zf.common.admin.model.sys.DeptAddMo;
import com.yun.zf.common.admin.model.sys.DeptEditMo;
import com.yun.zf.common.admin.model.sys.DeptSearchMo;
import com.yun.zf.common.annotation.LogPrint;
import com.yun.zf.common.constant.UserConstants;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 部门信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dept")
@Api(tags = "合作伙伴管理")
public class SysDeptController
{
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private UploadDeptListener deptListener;


    @GetMapping("/treeselect")
    @ApiOperation(value = "部门下拉列表")
    public Response treeselect(Long provinceCode,Long cityCode, int deep)
    {
        List<SysDept>  deptList = deptService.selectDeptList(provinceCode,cityCode);
        List<Tree<String>>  depts = deptService.editDeptTree(deptList,deep);
        return Response.success(depts);
    }


    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @PostMapping("/oneLevel")
    @ApiOperation(value = "一级部门列表")
    @LogPrint(description = "一级部门列表")
    public Response oneLevel(@RequestBody DeptSearchMo dept)
    {
        return Response.success(deptService.getDeptTree(dept));
    }





    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping(value = "/{deptId}")
    @ApiOperation(value = "合作伙伴详情")
    public Response getInfo(@PathVariable Long deptId)
    {
       SysDept dept = deptService.getDeptByDeptId(deptId);
        return Response.success(dept);
    }

    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @PostMapping("/add")
    @ApiOperation(value = "新增合作伙伴")
    public Response add(@Validated @RequestBody DeptAddMo dept)
    {
        int sum = deptService.insertDept(dept);
        // 验证当前分公司下是否存在此部门
        if (sum==-1)
        {
            return Response.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }

        if (sum>0){
            return  Response.success("部门添加成功");
        }

        return Response.error("部门添加失败");

    }
    /**
     * 修改部门
     */
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @PostMapping("/edit")
    @ApiOperation(value = "合作伙伴编辑")
    public Response edit(@Validated @RequestBody DeptEditMo dept)
    {
         Long parentId = StringUtils.isNull(dept.getParentId()) ? 0L :dept.getParentId();
        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept.getDeptId(),parentId,dept.getDeptName())))
        {
            return Response.error("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        int count =  deptService.updateDept(dept);
        if (count>0){
            return Response.success("修改成功");
        }
        return Response.success("修改失败");
    }





    @PostMapping(value = "/excelDept")
    @ApiOperation(value = "门店导入")
    @LogPrint(description = "门店导入")
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    public Response excelDept(@RequestParam(value="deptFile", required = false) MultipartFile file)
    {
        try {
            //获得工作簿
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), DeptAddMo.class, deptListener);
            //获得工作表  又两种形形式可以通过下标也可以通过名字2003Excel不支持名字
            ExcelReaderSheetBuilder sheet = read.sheet();
            //进行读操作
            sheet.doRead();
            return Response.success();
        }catch (Exception e){
            return Response.error(e.getMessage());
        }
    }


    @GetMapping(value = "/excelDept")
    @ApiOperation(value = "合作伙伴状态修改")
    @LogPrint(description = "合作伙伴状态修改")
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    public Response chanageStatus(Long deptId,int status,int level )
    {
        try {
            deptService.changeStatus(deptId,status,level);
        }catch (Exception e){
            return  Response.error("操作失败");
        }
        return  Response.success("操作成功");
    }


}
